package org.jivesoftware.smackx;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smackx.packet.DataForm;

public class Form
{
  public static final String TYPE_FORM = "form";
  public static final String TYPE_SUBMIT = "submit";
  public static final String TYPE_CANCEL = "cancel";
  public static final String TYPE_RESULT = "result";
  public static final String NAMESPACE = "jabber:x:data";
  public static final String ELEMENT = "x";
  private DataForm dataForm;

  public static Form getFormFrom(Packet paramPacket)
  {
    PacketExtension localPacketExtension = paramPacket.getExtension("x", "jabber:x:data");
    if (localPacketExtension != null)
    {
      DataForm localDataForm = (DataForm)localPacketExtension;
      if (localDataForm.getReportedData() == null)
        return new Form(localDataForm);
    }
    return null;
  }

  public Form(DataForm paramDataForm)
  {
    this.dataForm = paramDataForm;
  }

  public Form(String paramString)
  {
    this.dataForm = new DataForm(paramString);
  }

  public void addField(FormField paramFormField)
  {
    this.dataForm.addField(paramFormField);
  }

  public void setAnswer(String paramString1, String paramString2)
  {
    FormField localFormField = getField(paramString1);
    if (localFormField == null)
      throw new IllegalArgumentException("Field not found for the specified variable name.");
    if ((!"text-multi".equals(localFormField.getType())) && (!"text-private".equals(localFormField.getType())) && (!"text-single".equals(localFormField.getType())) && (!"jid-single".equals(localFormField.getType())) && (!"hidden".equals(localFormField.getType())))
      throw new IllegalArgumentException("This field is not of type String.");
    setAnswer(localFormField, paramString2);
  }

  public void setAnswer(String paramString, int paramInt)
  {
    FormField localFormField = getField(paramString);
    if (localFormField == null)
      throw new IllegalArgumentException("Field not found for the specified variable name.");
    setAnswer(localFormField, Integer.valueOf(paramInt));
  }

  public void setAnswer(String paramString, long paramLong)
  {
    FormField localFormField = getField(paramString);
    if (localFormField == null)
      throw new IllegalArgumentException("Field not found for the specified variable name.");
    if ((!"text-multi".equals(localFormField.getType())) && (!"text-private".equals(localFormField.getType())) && (!"text-single".equals(localFormField.getType())))
      throw new IllegalArgumentException("This field is not of type long.");
    setAnswer(localFormField, Long.valueOf(paramLong));
  }

  public void setAnswer(String paramString, float paramFloat)
  {
    FormField localFormField = getField(paramString);
    if (localFormField == null)
      throw new IllegalArgumentException("Field not found for the specified variable name.");
    if ((!"text-multi".equals(localFormField.getType())) && (!"text-private".equals(localFormField.getType())) && (!"text-single".equals(localFormField.getType())))
      throw new IllegalArgumentException("This field is not of type float.");
    setAnswer(localFormField, Float.valueOf(paramFloat));
  }

  public void setAnswer(String paramString, double paramDouble)
  {
    FormField localFormField = getField(paramString);
    if (localFormField == null)
      throw new IllegalArgumentException("Field not found for the specified variable name.");
    if ((!"text-multi".equals(localFormField.getType())) && (!"text-private".equals(localFormField.getType())) && (!"text-single".equals(localFormField.getType())))
      throw new IllegalArgumentException("This field is not of type double.");
    setAnswer(localFormField, Double.valueOf(paramDouble));
  }

  public void setAnswer(String paramString, boolean paramBoolean)
  {
    FormField localFormField = getField(paramString);
    if (localFormField == null)
      throw new IllegalArgumentException("Field not found for the specified variable name.");
    if (!"boolean".equals(localFormField.getType()))
      throw new IllegalArgumentException("This field is not of type boolean.");
    setAnswer(localFormField, paramBoolean ? "1" : "0");
  }

  private void setAnswer(FormField paramFormField, Object paramObject)
  {
    if (!isSubmitType())
      throw new IllegalStateException("Cannot set an answer if the form is not of type \"submit\"");
    paramFormField.resetValues();
    paramFormField.addValue(paramObject.toString());
  }

  public void setAnswer(String paramString, List<String> paramList)
  {
    if (!isSubmitType())
      throw new IllegalStateException("Cannot set an answer if the form is not of type \"submit\"");
    FormField localFormField = getField(paramString);
    if (localFormField != null)
    {
      if ((!"jid-multi".equals(localFormField.getType())) && (!"list-multi".equals(localFormField.getType())) && (!"list-single".equals(localFormField.getType())) && (!"text-multi".equals(localFormField.getType())) && (!"hidden".equals(localFormField.getType())))
        throw new IllegalArgumentException("This field only accept list of values.");
      localFormField.resetValues();
      localFormField.addValues(paramList);
    }
    else
    {
      throw new IllegalArgumentException("Couldn't find a field for the specified variable.");
    }
  }

  public void setDefaultAnswer(String paramString)
  {
    if (!isSubmitType())
      throw new IllegalStateException("Cannot set an answer if the form is not of type \"submit\"");
    FormField localFormField = getField(paramString);
    if (localFormField != null)
    {
      localFormField.resetValues();
      Iterator localIterator = localFormField.getValues();
      while (localIterator.hasNext())
        localFormField.addValue((String)localIterator.next());
    }
    else
    {
      throw new IllegalArgumentException("Couldn't find a field for the specified variable.");
    }
  }

  public Iterator<FormField> getFields()
  {
    return this.dataForm.getFields();
  }

  public FormField getField(String paramString)
  {
    if ((paramString == null) || (paramString.equals("")))
      throw new IllegalArgumentException("Variable must not be null or blank.");
    Iterator localIterator = getFields();
    while (localIterator.hasNext())
    {
      FormField localFormField = (FormField)localIterator.next();
      if (paramString.equals(localFormField.getVariable()))
        return localFormField;
    }
    return null;
  }

  public String getInstructions()
  {
    StringBuilder localStringBuilder = new StringBuilder();
    Iterator localIterator = this.dataForm.getInstructions();
    while (localIterator.hasNext())
    {
      localStringBuilder.append((String)localIterator.next());
      if (localIterator.hasNext())
        localStringBuilder.append("\n");
    }
    return localStringBuilder.toString();
  }

  public String getTitle()
  {
    return this.dataForm.getTitle();
  }

  public String getType()
  {
    return this.dataForm.getType();
  }

  public void setInstructions(String paramString)
  {
    ArrayList localArrayList = new ArrayList();
    StringTokenizer localStringTokenizer = new StringTokenizer(paramString, "\n");
    while (localStringTokenizer.hasMoreTokens())
      localArrayList.add(localStringTokenizer.nextToken());
    this.dataForm.setInstructions(localArrayList);
  }

  public void setTitle(String paramString)
  {
    this.dataForm.setTitle(paramString);
  }

  public DataForm getDataFormToSend()
  {
    if (isSubmitType())
    {
      DataForm localDataForm = new DataForm(getType());
      Iterator localIterator = getFields();
      while (localIterator.hasNext())
      {
        FormField localFormField = (FormField)localIterator.next();
        if (localFormField.getValues().hasNext())
          localDataForm.addField(localFormField);
      }
      return localDataForm;
    }
    return this.dataForm;
  }

  private boolean isFormType()
  {
    return "form".equals(this.dataForm.getType());
  }

  private boolean isSubmitType()
  {
    return "submit".equals(this.dataForm.getType());
  }

  public Form createAnswerForm()
  {
    if (!isFormType())
      throw new IllegalStateException("Only forms of type \"form\" could be answered");
    Form localForm = new Form("submit");
    Iterator localIterator1 = getFields();
    while (localIterator1.hasNext())
    {
      FormField localFormField1 = (FormField)localIterator1.next();
      if (localFormField1.getVariable() != null)
      {
        FormField localFormField2 = new FormField(localFormField1.getVariable());
        localFormField2.setType(localFormField1.getType());
        localForm.addField(localFormField2);
        if ("hidden".equals(localFormField1.getType()))
        {
          ArrayList localArrayList = new ArrayList();
          Iterator localIterator2 = localFormField1.getValues();
          while (localIterator2.hasNext())
            localArrayList.add((String)localIterator2.next());
          localForm.setAnswer(localFormField1.getVariable(), localArrayList);
        }
      }
    }
    return localForm;
  }
}

/* Location:           D:\hygieia\userclient\common\libs\easemobchat_2.1.8.jar
 * Qualified Name:     org.jivesoftware.smackx.Form
 * JD-Core Version:    0.6.1
 */